package com.yc.thread.conproExecutor;

import com.yc.thread.conpro.Consumer;

import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        // 缓冲区容器
        BlockingQueue<DataInfo> queue = new LinkedBlockingDeque<>(10);
        // 生产者
        ProviderOne p1 = new ProviderOne(queue);
        ProviderOne p2 = new ProviderOne(queue);
        ProviderOne p3 = new ProviderOne(queue);

        // 消费者
        ConsumerOne c1 = new ConsumerOne(queue);
        ConsumerOne c2 = new ConsumerOne(queue);
        ConsumerOne c3 = new ConsumerOne(queue);

        // 创建线程池运行，缓存线程池，可以创建无穷大的线程，没有任务的时候不创建线程，空闲线程存活时间为60s(默认)
        ExecutorService es = Executors.newCachedThreadPool();
        es.execute(p1);
        es.execute(p2);
        es.execute(p3);
        es.execute(c1);
        es.execute(c2);
        es.execute(c3);

        Thread.sleep(3000);

        p1.stop();
        p2.stop();
        p3.stop();

        Thread.sleep(2000);
    }
}
